<?php
namespace app\farm\controller;
use app\AdminRest;
use app\ApiRest;
use app\farm\model\BagAtv;
use app\farm\model\Coupon;
use app\farm\model\CouponAtv;
use app\farm\model\CouponAtvRecord;
use app\farm\model\CouponRecord;
use think\App;
use app\shop\model\Order as Model;
use think\facade\Db;


class IndexCoupon extends ApiRest
{


    protected $model;

    protected $atv_model;

    protected $record_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Coupon();

        $this->atv_model = new BagAtv();

        $this->record_model = new CouponRecord();

    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-02 11:07
     * @功能说明:正在进行中的福包活动
     */
    public function bagIngInfo(){

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['start_time','<',time()];

        $dis[] = ['end_time','>',time()];

        $data = $this->atv_model->dataInfo($dis);

        if(empty($data)){

            $this->errorMsg('活动已结束');
        }

        $where = [

            'atv_id' => $data['id'],

            'user_id'=> $this->getUserId(),

            'status' => 1
        ];
        //查看自己是否领取过
        $user_record = $this->record_model->dataInfo($where);

        if(!empty($user_record)){
            //已经领取
            $data['active_status'] = 2;

        }elseif ($data['user_num']<$data['have_num']){
            //已经领完
            $data['active_status'] = 3;

        }else{
            //进行中
            $data['active_status'] = 1;
        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-02 13:44
     * @功能说明:新客户获取活动优惠券
     */
    public function getAtvCoupon(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->atv_model->dataInfo($dis);

        if(empty($data)){

            $this->errorMsg('活动已结束');
        }

        $where = [

            'atv_id' => $data['id'],

            'user_id'=> $this->getUserId(),

            'status' => 1
        ];
        //查看自己是否领取过
        $user_record = $this->record_model->dataInfo($where);

        if(empty($user_record)){

            $this->errorMsg('你已经领取过了');
        }
        //添加领取记录
        Db::startTrans();

        $insert = [

            'uniacid'   => $this->_uniacid,

            'get_user'  => $this->getUserId(),

            'share_user'=> $input['share_user'],

            'atv_id'    => $data['id'],

            'user_coupon'=> $data['user_coupon'],

            'user_coupon_num'=> $data['user_coupon_num'],

            'share_coupon'=> $data['share_coupon'],
        ];

        $res = $this->record_model->dataAdd($insert);

        if($res==0){

            Db::rollback();

            $this->errorMsg('领取失败');
        }

        $id = $this->record_model->getLastInsID();

        $coupon_record_model = new CouponRecord();
        //给新客户发优惠券
        $res = $coupon_record_model->recordAdd($data['user_coupon'],$insert['get_user'],$id);

        if(!empty($res['code'])){

            Db::rollback();

            $this->errorMsg($res['msg']);
        }

        Db::commit();

        return $this->success($res);
    }

















}
